/**
 * Created by ouy on 2016/8/3.
 */
var mongoose = require('mongoose');
var MovieSchema = new mongoose.Schema({
    doctor: String,
    title: String,
    language: String,
    country: String,
    summary: String,
    flash: String,
    poster: String,
    year: Number,
    meta:{
        createAt: {
            type:Date,
            default: Date.now()
        },
        updateAt: {
            type: Date,
            default: Date.now()
        }
    }
})
// 存数据的时候调用次方法判断是否为新数据
MovieSchema.pre('save', function(next){
    if (this.isNew){
        this.meta.createAt = this.meta.updateAt = Date.now();
    }else{
        this.meta.updateAt = Date.now();
    }

    next();
})

MovieSchema.statics = {
    fetch: function(cb){//此方法取出数据库里所有数据
        return this
            .find({})
            .sort('meta.updateAt')
            .exec(cb)
    },
    findById: function(id, cb){ //单条数据
        return this
            .findOne({_id: id})
            .exec(cb)
    }
}

//模式 导出
module.exports = MovieSchema;